﻿using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using LINQtoFxCop.Core;
using Microsoft.FxCop.Sdk;

namespace LINQtoFxCop.Metrics.Methods
{
  public class MethodNbOverloads : MetricsIntrospectionRule
  {
    protected MethodNbOverloads(string name, string resourceName, Assembly assembly)
      : base(name, resourceName, assembly)
    { }
    public MethodNbOverloads()
      : base("MethodNbOverloads")
    { }

    protected override IEnumerable<QueryResult> Query()
    {
      return
        Methods
          .Where(method => method.NbOverloads() > MetricThreshold())
        //.Distinct(new MethodComparerForNbOverloads())
          .Select(method => Result(method, method.NbOverloads()));
    }

    private class MethodComparerForNbOverloads : IEqualityComparer<Method>
    {
      public bool Equals(Method x, Method y)
      {
        return x.Name.Name.Equals(y.Name.Name);
      }

      public int GetHashCode(Method obj)
      {
        return obj.Name.Name.GetHashCode();
      }
    }
  }
}
